//
//  LCMineViewController.m
//  JK九酷云头条
//
//  Created by jiukuyinyue on 2017/11/3.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LCMineViewController.h"

@interface LCMineViewController ()

@end

@implementation LCMineViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.wx_bind = @"";
    self.credit = @"";
    self.cash = @"";
    if ([[_UserM usercode] isEqualToString:@"bg16ggf1k2u07q2fx0qx"]||[[_UserM usercode] isEqualToString:@""]) {
        self.arrayRemark = @[@"可轻松获得几千金币",@"",@""];
    }else{
        self.arrayRemark = @[@"可轻松获得几千金币",@"在领取0.5元红包",@"再领取0.5元红包"];
    }
    if([[_UserM usercode] isEqualToString:@""]){
        [self.tableViewMine reloadData];
    }else{
        [self initWirhGetMsg];
    }
}
-(void)initWirhGetMsg
{
    NSString * str = [NSString stringWithFormat:@"%@/user/get_info",URLTotal];
    NSDictionary * dic = [[NSDictionary alloc]init];
    [LSZRequestData initregistrationSendVerificationCode:dic url:str :^(BOOL isOK, NSString * str, NSDictionary * dic) {
        if (isOK == YES) {
            self.userName = [NSString stringWithFormat:@"%@",dic[@"data"][@"username"]];
            self.uid = [NSString stringWithFormat:@"%@",dic[@"data"][@"uid"]];
            self.wx_bind = [NSString stringWithFormat:@"%@",dic[@"data"][@"wx_bind"]];
            self.credit = [NSString stringWithFormat:@"%@",dic[@"data"][@"credit"]];
            self.cash = [NSString stringWithFormat:@"%@",dic[@"data"][@"cash"]];
             [self.tableViewMine reloadData];
           }else{
            [SVProgressHUD showErrorWithStatus:dic[@"msg"]];
        }
    }];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [SVProgressHUD dismiss];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;

//    if (@available(iOS 11.0, *)) {
//        self.navigationController.navigationBar.prefersLargeTitles = NO;
//    }
//    else
//    {
//        self.navigationController.delegate = self;
//    }
    self.arrayTitle = @[@"新手任务",@"输入邀请码",@"绑定微信",@"常见问题",@"兑换提现",@"收入明细",@"历史记录"];

    [self.tableViewMine registerClass:[LSZMineHeaderCell class] forHeaderFooterViewReuseIdentifier:@"LSZMineHeaderCell"];
    [self.tableViewMine registerClass:[LSZMineTableViewCell class] forCellReuseIdentifier:@"LSZMineTableViewCell"];
    [self.tableViewMine registerClass:[LSZInvitePrizeTableViewCell class] forCellReuseIdentifier:@"LSZInvitePrizeTableViewCell"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0){
        return 8;
    }else{
        return 1;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 210;
    }else{
        return 5;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0 && indexPath.section == 0){
        return 60;
    }
    return 45;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        LSZMineHeaderCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"LSZMineHeaderCell"];
        if([[_UserM usercode] isEqualToString:@""]){
             cell.labelGoldCOINS.text = @"金币:0";
            cell.labelMoney.text = @"收益:0";
            cell.labelName.text = @"";
        }else{
             cell.labelGoldCOINS.text = [NSString stringWithFormat:@"金币: %@",self.credit];
            if ([[_UserM usercode] isEqualToString:@"bg16ggf1k2u07q2fx0qx"]) {
                cell.labelMoney.text = [NSString stringWithFormat:@"收益:%@",self.cash];
            }else{
                cell.labelMoney.text = [NSString stringWithFormat:@"零钱:%@",self.cash];
            }

            if ( [self.userName  isEqualToString:@""] ||[self.userName isEqualToString:@"(null)"]||self.userName == nil || self.userName == NULL||[self.userName isKindOfClass:[NSNull class]]||[[self.userName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
                cell.labelName.text = [NSString stringWithFormat:@"jk_%@",self.uid];
            }else{
                cell.labelName.text = [NSString stringWithFormat:@"%@", self.userName];
            }
        }
         UITapGestureRecognizer * singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImageViewTap)];
         [cell.imageViewHeaderPortrait addGestureRecognizer:singleTap];
        return cell;
    }else{
        UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        if (!cell ) {
            cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"cell"];
        }
        return cell;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0&&indexPath.section == 0) {
        LSZInvitePrizeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZInvitePrizeTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else{
        LSZMineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZMineTableViewCell" forIndexPath:indexPath];
        if(indexPath.section == 0){
            cell.labelTitle.text = [NSString stringWithFormat:@"%@",self.arrayTitle[indexPath.row-1]];
            if (indexPath.row>0&&indexPath.row <3) {
                cell.labelRemark.hidden = NO;
                cell.labelRemark.text = [NSString stringWithFormat:@"%@",self.arrayRemark[indexPath.row-1]];
            }
            else if (indexPath.row == 3){
                cell.labelRemark.hidden = NO;
                if ([self.wx_bind isEqualToString:@""]||[self.wx_bind isEqualToString:@"0"]) {
                    cell.labelRemark.text = [NSString stringWithFormat:@"%@",self.arrayRemark[indexPath.row-1]];
                    cell.labelRemark.textColor = [UIColor redColor];
                }
                else{
                    cell.labelRemark.text = @"已绑定";
                    cell.labelRemark.textColor = [UIColor grayColor];
                }
            }
            else{
                cell.labelRemark.hidden = YES;
            }
        }else{
            cell.labelTitle.text = @"设置";
            cell.labelRemark.hidden = YES;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        [self initWithOneSection:indexPath];
    }else{
        NSLog(@"设置");
        [self gotoSettingVC];
    }
 
}
-(void)gotoSettingVC
{
    //设置
    SettingViewController * VC = [[SettingViewController alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - 第一个区
-(void)initWithOneSection :(NSIndexPath *)indexPath
{
    if (indexPath.row == 4) {
        [self initWithH5:@"http://toutiao.app.inping.com/user/questions" :@"常见问题"];//常见问题
    }
    else{
        if ([[_UserM usercode] isEqualToString:@""]) {
            [self gotoLogIn];
        }
        else{
            if (indexPath.row == 0) {
                NSString * url = [[NSString alloc]initWithFormat:@"http://toutiao.app.inping.com/user/apprentice?usercode=%@",[_UserM usercode]];
                [self initWithH5:url :@"邀请有奖"];
                return;
            }
            if (indexPath.row == 1){
                NSString * url = [[NSString alloc]initWithFormat:@"http://toutiao.app.inping.com/user/task?usercode=%@",[_UserM usercode]];
                [self initWithH5:url :@"新手任务"];
                return;
            }
            if (indexPath.row == 2) {
                NSString * url = [[NSString alloc]init];
                 url = [[NSString alloc]initWithFormat:@"http://toutiao.app.inping.com/user/invitation_code?usercode=%@",[_UserM usercode]];
                [self initWithH5:url :@"邀请码"];
                return;
            }
          
            if (indexPath.row == 3&&([self.wx_bind isEqualToString:@"0"]||[self.wx_bind isEqualToString:@""])) {
                //        [self initWithH5:@"http://toutiao.app.inping.com/user/invitation_code"];
                NSString *clzName = @"LCWeixinLoginVC";
                Class clz = NSClassFromString(clzName);//将字符串转化成class
                UIViewController *viewController = [[clz alloc] init];
                [self.navigationController pushViewController:viewController animated:YES];
                return;
            }
            if (indexPath.row == 3&&(![self.wx_bind isEqualToString:@"0"]&&![self.wx_bind isEqualToString:@""])) {
                NSLog(@"已绑定微信");
                [SVProgressHUD showSuccessWithStatus:@"已绑定微信"];
                return;
            }
            if (indexPath.row == 5) {
                NSString * url = [[NSString alloc]initWithFormat:@"http://toutiao.app.inping.com/user/withdraw?usercode=%@",[_UserM usercode]];
                [self initWithH5:url :@"提现"];
                return;
            }
            if (indexPath.row == 6) {
                NSString * url = [[NSString alloc]initWithFormat:@"http://toutiao.app.inping.com/user/incomes?usercode=%@",[_UserM usercode]];
                [self initWithH5:url :@"收入明细"];
                return;
            }
            if (indexPath.row == 7) {
                NSString * url = [[NSString alloc]initWithFormat:@"http://toutiao.app.inping.com/user/history/1?usercode=%@",[_UserM usercode]];
                [self initWithH5:url :@"历史记录"];
                return;
            }
        }
    }
}
-(void)clickImageViewTap
{
    if ([[_UserM usercode] isEqualToString:@""]) {
        [self gotoLogIn];
    }
    else{
        [self gotoSettingVC];
    }
}
#pragma mark - 跳转h5界面
-(void)initWithH5 :(NSString *)url :(NSString*)title
{
    LSZVideoDetailsWebView * vc = [[LSZVideoDetailsWebView alloc]init];
    vc.url = url;
    vc.titleStr = title;
    vc.isDistinguish = @"2";
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - 创建表
-(UITableView *)tableViewMine
{
    if (_tableViewMine == nil) {
       
        _tableViewMine = [[UITableView alloc]initWithFrame:CGRectMake(0, -STATUS_BAR_HEIGHT, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
        _tableViewMine.delegate = self;
        _tableViewMine.dataSource = self;
        //添加以下代码关闭估算行高
        _tableViewMine.estimatedRowHeight = 0;
        _tableViewMine.estimatedSectionHeaderHeight = 0;
        _tableViewMine.estimatedSectionFooterHeight = 0;
        [self.view addSubview:_tableViewMine];
    }
    return _tableViewMine;
}

// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
#pragma mark- 跳转登陆界面
-(void)gotoLogIn
{
    if([[_UserM usercode] isEqualToString:@""]){
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"AccLogin" bundle:nil];
        UIViewController *controller = [sb instantiateInitialViewController];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

@end
